layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 商品表管理
     */
    var BasicGoods = {
        tableId: "basicGoodsTable"
    };

    /**
     * 初始化表格的列
     */
    BasicGoods.initColumn = function () {
        return [[
            {type: 'checkbox',fixed: 'left'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'goodsCustomerName', sort: true, title: '所属客户',fixed: 'left'},
            {field: 'goodsName', sort: true, title: '商品名称',fixed: 'left'},
            {field: 'goodsNumber', sort: true, title: '商品编号',fixed: 'left'},
            {field: 'goodsUnit', sort: true, title: '单位'},
            {field: 'goodsBigtype', sort: true, title: '商品大类'},
            {field: 'goodsType', sort: true, title: '商品类别'},
            {field: 'goodsVolume', sort: true, title: '体积（M3）'},
            {field: 'goodsCharging', sort: true, title: '计费指定价'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicGoods.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(BasicGoods.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicGoods.tableId,
        url: Feng.ctxPath + '/basicGoods/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BasicGoods.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicGoods.search();
    });

    // 批量分配物料编码
    $('#btnAssign').click(function () {
        BasicGoods.btnAssign();
    });

    //路径
    function getpage(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        } else {
            return null;
        }
    };
    
    //分配物料编码
    function confirm(data){
        var index;
        $.ajax({
        	url:Feng.ctxPath + "/basicPositionmaterial/assignMaterial?goodsId="+data.id+"&ids="+getpage("ids"),
        	cache: false,
        	dataType:"json",
        	beforeSend:function(){
            	//弹出加载中
        		index = layer.load(1, {
                    shade: [0.3,'#000'] //0.1透明度的白色背景
                });
        	},
        	success:function(data){
            	layer.close(index); 
            	var Indexs = parent.layer.getFrameIndex(window.name);
                parent.layer.close(Indexs);
            	Feng.success("分配成功！");
        	},
        	error:function(data){
            	layer.close(index); 
                Feng.error("分配失败！" + data.responseJSON.message)
        	}
        })
        return false;
    };
    
    // 批量分配物料编码
    BasicGoods.btnAssign = function () {
    	var checkStatus = table.checkStatus('basicGoodsTable');
        data = checkStatus.data;
        if (data.length>0) {
            var gids = [];
            for (var i = 0; i < data.length; i++) {
            	gids.push(data[i].id);
			}
            layer.confirm('是否批量分配选中物料编码?', function(index){
            	layer.close(index);
                $.ajax({
                	url:Feng.ctxPath + "/basicPositionmaterial/btnassignMaterial?gids="+gids+"&ids="+getpage("ids"),
                	cache: false,
                	dataType:"json",
                	beforeSend:function(){
                    	//弹出加载中
                		index = layer.load(1, {
                            shade: [0.3,'#000'] //0.1透明度的白色背景
                        });
                	},
                	success:function(data){
                    	layer.close(index); 
                		if (data.success) {
                        	var Indexs = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(Indexs);
                        	Feng.success("分配成功!");
						} else {
                            Feng.error("分配失败！" + data.message)
						}
                	},
                	error:function(data){
                    	layer.close(index); 
                        Feng.error("分配失败！" + data.responseJSON.message)
                	}
                })
            }); 
		} else {
			layer.alert('请至少选中一条数据');
		}
    };
    
    // 双击行事件
    table.on('rowDouble(' + BasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
    	confirm(data);
    });
});
